package vivace.view;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.sound.midi.MidiDevice;
import javax.sound.midi.MidiUnavailableException;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;

import vivace.helper.GUIHelper;
import vivace.model.App;

public class PreferencesFrame extends JDialog implements ActionListener  {

	private JComboBox inCb, outCb;
	private static final long serialVersionUID = 1986463042802326674L;

	/**
	 * The frame where the user choose MIDI devices
	 * @param frame		Reference to the frame where the frame should pop up in (in middle)
	 */
	public PreferencesFrame(Frame frame) {

		super(frame,true);

		//Titel and small properties the frame
		setTitle(lib.Texter.Text.getText("MIDI_PREFERENCES"));
		setLocationRelativeTo(null);
    	setResizable(false);
    	
    	//Container for the frame
		Container c = getContentPane();
		c.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
		
		GridLayout layout = new GridLayout(3,2);
		layout.setVgap(5);
		
		JPanel p = new JPanel(layout);
	
		//Inbox with the in-device elements
		inCb = new JComboBox(App.getMidiInDevices());
		inCb.setRenderer(new MidiDeviceRenderer());

		//Outbox with the out-device elements
		outCb = new JComboBox(App.getMidiOutDevices());
		outCb.setRenderer(new MidiDeviceRenderer());

		JLabel inLabel = new JLabel("MIDI IN");
		JLabel outLabel = new JLabel("MIDI OUT");

		Font f = inLabel.getFont();

		// bold
		inLabel.setFont(f.deriveFont(f.getStyle() ^ Font.BOLD));
		outLabel.setFont(f.deriveFont(f.getStyle() ^ Font.BOLD));

		p.add(inLabel);
		p.add(inCb);

		p.add(outLabel);
		p.add(outCb);
		
		c.add(p);
		
		/* OK Button */
		JButton okButton = new JButton("OK");
		okButton.setActionCommand("ok");
		okButton.addActionListener(this);
		c.add(okButton);
		
		pack();
	}

	/**
	 * What happens when choosing new element in combobox, in/out-device
	 */
	public void actionPerformed(ActionEvent e) {
		String cmd = e.getActionCommand();
		
		if (cmd.equals("ok")) {
			/* apply changes */
			MidiDevice inDevice, outDevice;
			
			inDevice = (MidiDevice) inCb.getSelectedItem();
			outDevice = (MidiDevice) outCb.getSelectedItem();
			
			if (inDevice != null) {
				try {
					App.Project.updateInDevice(inDevice);
				} catch (MidiUnavailableException ex) {
					GUIHelper.displayError("E_MIDI_DEVICE_UNAVAILABLE");
					return;
				}
			}
			
			if (outDevice != null) {
				try {
					App.Project.updateOutDevice(outDevice);
				} catch (MidiUnavailableException ex) {
					GUIHelper.displayError("E_MIDI_DEVICE_UNAVAILABLE");
					return;
				}
			}
	
			setVisible(false);
			dispose();
		} 
	}

	/**
	 * Define what to show up in the comboBox
	 * @author petterg
	 *
	 */
	private class MidiDeviceRenderer implements ListCellRenderer {

		public Component getListCellRendererComponent(JList list, Object value,
				int index, boolean isSelected, boolean cellHasFocus) {

			MidiDevice d = (MidiDevice) value;

			if(d == null){
				return new JLabel();
			} else {
				return new JLabel(d.getDeviceInfo().getName());
			}
		}

	}

}
